package com.webdesign.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.webdesign.entity.TbUser;
import com.webdesign.service.ITbTermService;
import com.webdesign.service.ITbUserService;
import com.webdesign.util.InputExcelUtil;
import com.webdesign.util.ReturnTerm;

@Controller("TermManageAction")
@Scope("prototype")
public class TermManageAction  extends ActionSupport{
	private ITbTermService iTbTermService;
	
	private ITbUserService iTbUserService;
	
	private String termname;
	
	private File users;
	
	private String usersFileName;
	
	private String usersContentType;
	
	private String flag;
	
	private List<String> terms;
	
	/*
	 * 新建学年页面，返回几个可供选择的学年名称
	 */
	public String terms(){
		Map<String,Object> session = ActionContext.getContext().getSession();
		String termid = (String)session.get("termid");
		String termname = iTbTermService.getTermName(termid);
		terms = ReturnTerm.termNames(termname);
		return SUCCESS;
	}
	
	/*
	 * 新建学年页面，上传excel文件,初始化学生信息
	 */
	public String updateExcel(){
		
		//讲file类型的数据转化成inputstream传到后台接口操作
		InputStream fis = null;
		try {
			fis = new FileInputStream(users);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
		
		List<TbUser> tbUserList = null;
		//判断excel文件格式是2003还是2007，调用不同的接口，将excel里面的数据转化成bean返回
		if(usersFileName.contains("xlsx")){
			tbUserList = InputExcelUtil.importExcel2007(fis);
		}else if(usersFileName.contains("xls")){
			tbUserList = InputExcelUtil.importExcel2003(fis);
		}else{
			ActionContext.getContext().put("url", "/teacher/terms");
			return "redirect";
		}
		
		//判断学年是否存在，若不存在则新建，同时返回学年编号
		String termid = iTbTermService.jugeTerm(termname);
		iTbUserService.insertUsers(termid, tbUserList);
		ActionContext.getContext().put("url", "/teacher/showTermAndHomework");
		return "redirect";
	}

	public ITbTermService getiTbTermService() {
		return iTbTermService;
	}

	@Resource(name="TbTermService")
	public void setiTbTermService(ITbTermService iTbTermService) {
		this.iTbTermService = iTbTermService;
	}

	public ITbUserService getiTbUserService() {
		return iTbUserService;
	}

	@Resource(name="TbUserService")
	public void setiTbUserService(ITbUserService iTbUserService) {
		this.iTbUserService = iTbUserService;
	}

	public File getUsers() {
		return users;
	}

	public void setUsers(File users) {
		this.users = users;
	}

	public String getUsersFileName() {
		return usersFileName;
	}

	public void setUsersFileName(String usersFileName) {
		this.usersFileName = usersFileName;
	}

	public String getUsersContentType() {
		return usersContentType;
	}

	public void setUsersContentType(String usersContentType) {
		this.usersContentType = usersContentType;
	}

	public String getTermname() {
		return termname;
	}

	public void setTermname(String termname) {
		this.termname = termname;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public List<String> getTerms() {
		return terms;
	}

	public void setTerms(List<String> terms) {
		this.terms = terms;
	}
	
}
